package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.NewsDao;
import com.pojo.News;
import com.pojo.Question;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsService extends ServiceImpl<NewsDao ,News> {

    @Autowired
    NewsDao newsDao;
    /**
     * 分页查询新闻
     * @param page
     * @param limit
     * @param key
     * @param tpid
     * @return
     */
    public IPage types(Integer page , Integer limit,String key,String tpid ){
        QueryWrapper query = new QueryWrapper();
        if(StringUtil.isNotNull(key)){
            query.like("nstitle",key);
        }
        if(StringUtil.isNotNull(tpid)){
            query.eq("tpid",tpid);
        }
        IPage iPage = page(new Page<>(page,limit),query);
        return iPage;
    }

    public IPage getNewsPage(int page, int limit, String newstype) {
        Page p=new Page(page,limit);
        QueryWrapper<News> qw=new QueryWrapper<>();
        if (StringUtil.isNotNull(newstype)){
            qw.like("tpid",newstype);
        }
        qw.orderByDesc("nstime");
        qw.eq("nsstatus",1);
        qw.ne("status",1);
        qw.ne("showindex",1);
        IPage iPage=newsDao.selectPage(p,qw);
        return iPage;
    }

    public List getHotNews(String newstype) {
        QueryWrapper<News> qw=new QueryWrapper<>();
        qw.eq("tpid",newstype);
        qw.eq("status",1);
        qw.eq("nsstatus",1);
        qw.orderByDesc("nstime");
        qw.last("limit 3");
        return  newsDao.selectList(qw);
    }

    public List lunboNews(String newstype) {
        QueryWrapper<News> qw=new QueryWrapper<>();
        qw.eq("tpid",newstype);
        qw.eq("showindex",1);
        qw.orderByDesc("nstime");
        qw.eq("nsstatus",1);
        qw.last("limit 5");
        return  newsDao.selectList(qw);
    }
}
